import Vue from 'vue'
import { post2, post } from '@/service/request.js'

Vue.prototype.$provinceChange = provinceChange
Vue.prototype.$beforeAvatarUpload = beforeAvatarUpload

// 获取目的地
export function getDestination() {
  let p = new Promise(function (resolve, reject) {
    post('center_region/api/travel_agency/getdestinationinformation',).then(res => {
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
  return p
}

// 获取省份 和市 // 获取区
export function provinceChange(province) {
  let p = new Promise(function (resolve, reject) {

    // console.log(url, province);
    post("center_region/api/travel_agency/getregionlist", {
      cri_code: province,
    })
      .then((res) => {
        resolve(res);
      })
      .catch((res) => {
        reject(res);
      });
  });
  return p; // 返回一个promise
}



// 文件上传获取https://的图
export function beforeAvatarUpload(event, content) { // eslint-disable-line
  let p = new Promise(function (resolve, reject) {
    let file = event.target.files[0]
    // 1.不能上传空文件
    if (!file) return
    // 类型和大小判断
    const isJPG = file.type === "image/jpeg" || file.type === "image/png" || file.type === "image/jpg";
    const isLt5M = file.size / 1024 / 1024 < 2;

    if (!isJPG) {
      content.$message.error("请上传图片格式 🙁");
      return
    } else if (!isLt5M) {
      content.$message.error("上传图片大小不能超过 2MB!🙁");
      return
    }

    let fd = new FormData();
    fd.append("file[]", file); //传file[]  后台需要带下标
    // console.log(fd.get('file[]'));
    post2('center_region/api/travel_agency/uploadfile', fd).then(res => {
      resolve(res.data.data)
    }).catch(error => {
      reject(error)
    })
  })
  return p
}


// 上传视频
export function videoupload(evenfile, content) {
  // console.log(content, evenfile);
  let p = new Promise(function (resolve, reject) {
    // 1.不能上传空文件
    if (!evenfile) return
    // 类型和大小判断
    const isJPG = evenfile.type === "image/jpeg" || evenfile.type === "image/png" || evenfile.type === "image/jpg";
    // 大小限制
    const isLt100M = evenfile.size / 1024 / 1024 < 100;
    if (isJPG) {
      content.$message.error("请上传视频文件！🙁")
      return
    }
    if (!isLt100M) {
      content.$message.error("上传视频大小不能超过 100MB!🙁");
      return
    }

    let fd = new FormData();
    fd.append("file[]", evenfile); //传file[]  后台需要带下标
    post2('center_region/api/travel_agency/uploadfile', fd).then(res => {
      resolve(res.data.data[0])
      // console.log();
    }).catch(error => {
      reject(error)
    })
  })
  return p
}

// 图片看图
export function imgView(files) {
  let file = files.target.files[0];
  let imgurl = URL.createObjectURL(file);
  return imgurl
}

// 获取运营中心
export function getService(service) {
  let p = new Promise(function (resolve, reject) {
    post('center_region/api/travel_agency/getcenterregionlist', { city: service }).then(res => {
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
  return p
}


// 旅行社完善资料
export function information(data) {
  let p = new Promise(function (resolve, reject) {
    post('center_region/api/travel_agency/perfectinformation', data).then(res => {
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
  return p
}

// 简单登录封装
export function subBtn_mit(loginform) {
  let p = new Promise(function (resolve, reject) {
    post('center_region/api/travel_agency/login', loginform).then(res => {
      window.localStorage.setItem("token", res.data.data.token);
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
  return p
}

// 重设密码验证码
export function resetPasswordcode(phone) {
  let p = new Promise(function (resolve, reject) {
    post('center_region/api/travel_agency/accountresetpasswordsendsmscode', { "phone": phone }).then(res => {
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
  return p
}

// 获取服务列表
export function getCircuitList(data) {
  let p = new Promise(function (resolve, reject) {
    post("center_region/api/travel_agency/getrouteservicelist", data).then((res) => {
      resolve(res)
    }).catch(error => {
      reject(error)
    });
  })
  return p
}
// 获取线路详情 
export function getServiceDetails(route_id) {
  let p = new Promise(function (resolve, reject) {
    post('center_region/api/travel_agency/getrouteservicedetails', {
      route_service_id: route_id
    }).then(res => {
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
  return p
}

// 重设密码
export function resetPassword(data) {
  let p = new Promise(function (resolve, reject) {
    post('center_region/api/travel_agency/accountresetpassword', data).then(res => {
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
  return p
}

// 删除图片文案等操作
export function removeChild(event) {
  console.log(event);
}

// 发布路线

export function createCircuit(data) {
  let p = new Promise(function (resolve, reject) {
    post('center_region/api/travel_agency/setrouteservice', data).then(res => {
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
  return p
}


export function dupliCation(name) {
  let p = new Promise(function (resolve, reject) {
    post('center_region/api/travel_agency/checktravelagencynamerepeat', { travel_agency_name: name }).then(res => {
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
  return p
}

// 旅行社线路上架
export function routeontheshelves(id) {
  let p = new Promise(function (resolve, reject) {
    post('center_region/api/travel_agency/routeontheshelves', { route_service_id: id }).then(res => {
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
  return p
}

// 旅行社线路下架
export function routeofftheshelves(id) {
  let p = new Promise(function (resolve, reject) {
    post('center_region/api/travel_agency/routeofftheshelves', { route_service_id: id }).then(res => {
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
  return p
}

// 旅行社删除线路
export function routedeleted(id) {
  let p = new Promise(function (resolve, reject) {
    post('center_region/api/travel_agency/routedeleted', { route_service_id: id }).then(res => {
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
  return p
}

// 旅行社分销
export function setdistricircuit(data) {
  let p = new Promise(function (resolve, reject) {
    post('center_region/api/travel_agency/setdistributionrouteservice', data).then(res => {
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
  return p
}

// 获取发布的分销路线
export function getdistricircuit(data) {
  let p = new Promise(function (resolve, reject) {
    post('center_region/api/travel_agency/getminedistributionroutelist', data).then(res => {
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
  return p
}

// 设置我的分销线路是否开放分销
export function settingdistribution(data) {
  let p = new Promise(function (resolve, reject) {
    post('center_region/api/travel_agency/setminedistributionopenapplicationsetting', data).then(res => {
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
  return p
}

// 获取发布的分销线路详情 
export function getdistriedetails(data) {
  let p = new Promise(function (resolve, reject) {
    post('center_region/api/travel_agency/getdistributionroutedetails', {
      distribution_id: data
    }).then(res => {
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
  return p
}

// 分销下架
export function setdistriontheshelves(id) {

  let p = new Promise(function (resolve, reject) {
    post('center_region/api/travel_agency/setminedistributionrouteontheshelves', {
      distribution_id: id
    }).then(res => {
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
  return p
}

// 分销上架
export function setdistriofftheshelves(id) {

  let p = new Promise(function (resolve, reject) {
    post('center_region/api/travel_agency/setminedistributionrouteofftheshelves', {
      distribution_id: id
    }).then(res => {
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
  return p
}

// 获取分销市场线路列表
export function getdistributioncircuit(page_data) {
  let p = new Promise(function (resolve, reject) {
    post('center_region/api/travel_agency/getdistributionroutelist', page_data).then(res => {
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
  return p
}

// 获取我分销旅行社的线路列表
export function getdistributiononselflist(page_data) {
  let p = new Promise(function (resolve, reject) {
    post('center_region/api/travel_agency/getdistributiontravelagencyroutelist', page_data).then(res => {
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
  return p
}
